package com.example.jetpackcomposestate.todo

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import kotlin.random.Random

/**
 * @author huangzehui
 * @date 2/5/2024 下午11:24 周四
 * @description
 *
 * https://www.bilibili.com/video/BV1ob4y1a7ad?p=27&spm_id_from=pageDriver&vd_source=e07cc8cc94eb22bd76722071b03cd199
 */

fun generateRandomTodoItem(): TodoItem {
    val message = listOf(
        "Learn compose",
        "Learn state",
        "Build dynamic UIs",
        "Learn Unidirectional Data Flow",
        "Integrate LiveData",
        "Integrate ViewModel",
        "Remember to savedstate!",
        "Build stateless composables",
        "Use state from stateless composables"
    ).random()

    val icon = TodoIcon.entries.toTypedArray().random()
    return TodoItem(task = message, icon = icon)
}

@Composable
fun TodoScreen(
    items: List<TodoItem>,
    onAddItem: (todoItem: TodoItem) -> Unit,
    onRemoveItem: (todoItem: TodoItem) -> Unit
) {
    Column {

        TodoItemInputBackground(elevate = true) {
            TodoItemEntryInput(onAddItem)
        }

        LazyColumn(
            modifier = Modifier.weight(1f),
            contentPadding = PaddingValues(top = 8.dp)
        ) {
            items(count = items.size) {
                val item = items[it]
                TodoRow(
                    todoItem = item,
                    onItemClicked = onRemoveItem,
                    modifier = Modifier.fillParentMaxWidth(),
                )
            }
        }


        Button(
            onClick = {
                onAddItem(generateRandomTodoItem())
            }, modifier = Modifier
                .padding(16.dp)
                .fillMaxWidth()
        ) {
            Text(text = "Add random item")
        }
    }
}


@Composable
fun TodoRow(
    todoItem: TodoItem,
    onItemClicked: (TodoItem) -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier
            .clickable {
                onItemClicked(todoItem)
            }
            .padding(horizontal = 16.dp, vertical = 8.dp),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(text = todoItem.task)

        // 使用 remember 可以使这个值，在函数重组后，依旧保留上次生成的值
        // 类似 val k by lazy { }
        val iconAlpha: Float = remember(todoItem.id) {
            randomAlpha()
        }

        Icon(
            imageVector = todoItem.icon.imageVector,
            tint = LocalContentColor.current.copy(alpha = iconAlpha),
            contentDescription = stringResource(id = todoItem.icon.contentDescription)
        )
    }
}

private fun randomAlpha(): Float {
    return Random.nextFloat().coerceIn(0.3f, 0.9f)
}